<template>
    <el-dialog class="elDialog" :title="wintitle" :visible.sync="isShow" width="40%" :lock-scroll="true">
        <el-form ref="form" :model="order" :rules="rules" label-width="100px">
            <el-form-item label="接收人:" required prop="accept_id">
                <el-select v-model="order.accept_id" placeholder="请选择接收人" filterable>
                    <el-option v-for="item in acceptUserList" :key="item.id" :label="item.nick_name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="告警设备:" required prop="title">
                <el-input v-model="order.title" disabled></el-input>
            </el-form-item>
            <el-form-item label="产生原因:" required prop="content">
                <el-input v-model="order.content"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button type="info" @click="ignore">忽略</el-button>
                <!-- <el-button type="primary" @click="createPort">生成报告</el-button> -->
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import zyForm from '@/components/common/zyForm.vue'
import config1 from './formConfig'
import { requestApi, fileApi } from '@/requset/api'
export default {
    data() {
        return {
            wintitle: '生成工单',
            isShow: false,
            dialog: null,
            order: {
                user_id: 0,
                accept_id: null,
                title: '',
                content: '',
            },
            rules: {
                accept_id: [{ required: true, message: '请选择接收人' }],
                title: [{ required: true, message: '告警设备不能为空' }],
                content: [{ required: true, message: '产生原因不能为空' }],
            },
        }
    },
    components: { zyForm },
    methods: {
        doOpen(e) {
            let { device_id, device_name, max_warn_level, zone_id, factory_id, company_id, group_id, end_time, start_time } = e
            Object.assign(this.order, { device_id, zone_id, factory_id, company_id, group_id, end_time, start_time })
            this.order.title = device_name
            this.order.warn_level = max_warn_level
            this.warnPullList({ device_id }).then(() => (this.isShow = true))
        },
        //查询出可推送 工单 的用户列表
        warnPullList(obj) {
            return requestApi('post', 'warn/pull/list_1', obj, true).then((res) => {
                if (res.data.code == 0) {
                    this.acceptUserList = res.data.data
                } else {
                    this.acceptUserList = []
                }
            })
        },
        addNotice(obj) {
            requestApi('post', 'warn/add/notice', obj, true).then((res) => {
                if (res.data.code == 0) {
                    this.$message.success('成功添加一条工单')
                    this.isShow = false
                    this.$emit('search')
                    this.$EventBus.$emit('undateWarn')
                } else {
                    this.$message.info('创建失败')
                }
            })
        },
        submitForm() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.addNotice(this.order)
                } else {
                    this.$message.error('请补全表单！')
                    return false
                }
            })
        },
        ignore() {
            requestApi('post', 'warn/ignore_', this.order, true).then((res) => {
                if (res.data.code == 0) {
                    this.$message.success('操作成功')
                    this.isShow = false
                    this.$emit('search')
                } else {
                    this.$message.info('操作失败')
                }
            })
        },
        createPort() {
            let { device_id, end_time, start_time } = this.order
            requestApi(
                'post',
                'report/down/sys',
                {
                    type_: 1,
                    device_ids: [1],
                    times: ['2024-03-11 00:24:23', '2024-03-15 20:24:23'],
                },
                true,
            ).then((res) => {
                if (res.data.code == 0) {
                    this.$router.push(`/index/expdig/diareport`)
                }
            })
        },
    },
    created() {
        this.dialog = config1
        this.order.user_id = sessionStorage.getItem('user_id')
    },
}
</script>

<style></style>
